package com.huohuzhihui.client;

import com.huohuzhihui.config.ContractConfig;
import com.huohuzhihui.contract.Asset;
import com.huohuzhihui.contract.DigitalAsset;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple2;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple7;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Properties;

@Service
@NoArgsConstructor
@Data
public class DigitalAssetClient {

    static Logger logger = LoggerFactory.getLogger(DigitalAssetClient.class);

    @Autowired
    private Client client;

    private String address = "0xf07796fadd87b0f0d6759179e72673c93c7147e1";

    @Autowired
    private ContractConfig contractConfig;

    AssembleTransactionProcessor txProcessor;


    @PostConstruct
    public void init() throws Exception {
        this.txProcessor =
                TransactionProcessorFactory.createAssembleTransactionProcessor(
                        this.client, this.client.getCryptoSuite().getCryptoKeyPair());
    }


    public Tuple7<BigInteger, String, String, String, String, BigInteger, BigInteger> query(String assetId) {
        try {
            DigitalAsset asset = DigitalAsset.load(this.address, client, this.client.getCryptoSuite().getCryptoKeyPair());
            Tuple7<BigInteger, String, String, String, String, BigInteger, BigInteger> result = asset.select(assetId);
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            logger.error(" queryAssetAmount exception, error message is {}", e.getMessage());

            System.out.printf(" query asset account failed, error message is %s\n", e.getMessage());
        }
        return null;
    }

    public String register(String asset_id, String asset_name, String creator_address, String asset_hash, String asset_amount, String asset_price) {
        try {

          DigitalAsset asset = DigitalAsset.load(this.address, client, this.client.getCryptoSuite().getCryptoKeyPair());
            TransactionReceipt receipt = asset.register( asset_id,  asset_name,  creator_address,  asset_hash,  asset_amount,  asset_price);
            List<DigitalAsset.RegisterEventEventResponse> response = asset.getRegisterEventEvents(receipt);
            return receipt.getTransactionHash();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            e.printStackTrace();
            logger.error(" registerAssetAccount exception, error message is {}", e.getMessage());
            System.out.printf(" register asset account failed, error message is %s\n", e.getMessage());
        }
        return null;
    }


}
